package p.ithorns.framework.redis.stream.listener;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.stream.StreamListener;

/**
 * @Author: liuhaitao
 * @Date: 2022/12/8 15:16
 * @Description:
 */
public class RedisStreamGroupMessageListener implements StreamListener<String, MapRecord<String, String, String>> {

    private final static Logger log = LoggerFactory.getLogger(RedisStreamGroupMessageListener.class);

    private final String name;

    public RedisStreamGroupMessageListener(String name) {
        this.name = name;
    }

    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        // 接收到消息
        log.info("{}==> message id: {}", name, message.getId());
        log.info("{}==> stream: {}", name, message.getStream());
        log.info("{}==> body: {}", name, message.getValue());

        // 当是消费组消费时，如果不是自动ack，则需要在这个地方手动ack
        // redisTemplate.opsForStream() .acknowledge("key","group","recordId");
    }

}